import datetime
import configparser
import re
from impala.dbapi import connect
import time

config = configparser.ConfigParser()
config.read('./config.ini')


def handle_sql():
    # 生成sql语句
    sql = 'select count(*) from {table} where {condition};'
    month_pattern = re.compile('{month}')
    day_pattern = re.compile('{day}')
    hour_pattern = re.compile('{hour}')
    now = datetime.datetime.now()
    sqls = []
    for table_key in filter(lambda _: _.startswith('table-'), config.sections()):
        agos = config.getint(table_key, 'agos')
        target = now + datetime.timedelta(hours=-agos)
        month = target.strftime('%Y%m')
        day = target.strftime('%Y%m%d')
        hour = target.strftime('%Y%m%d%H')
        partation = config.get(table_key, 'partation')
        table = table_key.split('-')[1]
        partation = re.sub(month_pattern, month, partation)
        partation = re.sub(day_pattern, day, partation)
        partation = re.sub(hour_pattern, hour, partation)
        partation = partation.replace(',', ' and ')
        sqls.append(sql.format(table=table, condition=partation))

    return sqls


def execute_sql(sqls):
    # 执行sql语句
    host = config.get('common', 'host')
    port = config.getint('common', 'port')
    database = config.get('common', 'database')
    cli = connect(host=host, port=port, database=database)
    cursor = cli.cursor()
    # table,executeTime,count,useTime
    result = '{},{},{},{}\n'
    results = []
    now = datetime.datetime.now().strftime('%Y%m%d%H%M')
    for sql in sqls:
        start_time = time.time()
        cursor.execute(sql)
        count = cursor.fetchone()[0]
        use_time = time.time() - start_time
        table = sql.split(' ')[3]
        results.append(result.format(table, now, count, use_time))
    cursor.close()
    cli.close()
    return results


if __name__ == '__main__':
    sqls = handle_sql()
    result = execute_sql(sqls)
    now = datetime.datetime.now().strftime('%Y%m%d%H%M')

    with open(now, mode='w', encoding='urf-8') as file, \
            open('all', mode='a', encoding='utf-8') as all_file:
        file.writelines(result)
        all_file.writelines(result)
